package blog.db;

import java.util.Vector;
import blog.ConstString;
import blog.util.StringUtils;

public final class BlogAccountDB {
	private static StorageConnectWithIndex store;
	private static String errorString;
	
	public static boolean openDB()
	{
		if ( store==null )
		{
			try {
				Class myclass;
				myclass = new BlogAccount().getClass();
				store = StorageConnectWithIndex.open(ConstString.DB_NAME_BLOG_ACCOUNT, true, myclass);
			} catch (Exception e) {
				errorString = StringUtils.combin(ConstString.DB_ERR_COMMON_OPEN_FAILED, e.getMessage());
			}
		}
		return store==null;
	}
	
	public static void closeDB()
	{
		if ( store!=null )
		{
			store.close();
			store = null;
		}
	}

	public static boolean add( BlogAccount account )
	{
		if ( store!=null )
		{
			if ( !isExist(account) )
			{
				try {
					store.add(account);
					return true;
				} catch (Exception e) {
					errorString = ConstString.DB_ERR_COMMON_OPERATION_FAILED;
				}
			}
			else
			{
				errorString = StringUtils.combin(ConstString.DB_ERR_BLOG_ACCOUNT_EXIST_ALREADY, account.getAccountName());
			}
		}
		return false;
	}
	
	public static boolean update( BlogAccount account )
	{
		if ( store!=null )
		{
			try {
				store.update(account);
				return true;
			} catch (Exception e) {
				errorString = ConstString.DB_ERR_COMMON_OPERATION_FAILED;
			}
		}
		return false;
	}
	
	public static boolean delete( String accountName )
	{
		IStorableRecordWithIndex[] result;
		try {
			result = store.searchIndex(BlogAccount.FIELD_ACCOUNT_NAME, accountName);
			if ( result.length>0 )
			{
				store.delete(result[0]);
				BlogEntryDB.delete( result[0].getId( ) );
				return true;
			}
			else
			{
				errorString = StringUtils.combin(ConstString.DB_ERR_BLOG_ACCOUNT_NOT_EXIST, accountName);
			}
		} catch (Exception e1) {
			errorString = ConstString.DB_ERR_COMMON_OPERATION_FAILED;
		}
		return false;
	}
	
	public static BlogAccount[] getAll()
	{
		try {
			Vector result =  store.getAll();
			BlogAccount[] returnObj = new BlogAccount[result.size()];
			result.copyInto(returnObj);
			return returnObj;
		}
		catch( Exception e )
		{
			return new BlogAccount[0];
		}
	}
	
	public static BlogAccount[] getAllIndex()
	{
		Vector result =  store.getAllIndex();
		BlogAccount[] returnObj = new BlogAccount[result.size()];
		result.copyInto(returnObj);
		return returnObj;
	}
	
	public static BlogAccount get( String accountName )
	{
		if ( store!=null )
		{
			IStorableRecordWithIndex[] result;
			try {
				result = store.search(BlogAccount.FIELD_ACCOUNT_NAME, accountName);
				if ( result.length>0 )
				{
					return (BlogAccount) result[0];
				}
			} catch (Exception e1) {
			}
		}
		return null;
	}
	
	private static boolean isExist( String accountName )
	{
		try {
			return store.searchIndex(BlogAccount.FIELD_ACCOUNT_NAME, accountName).length>0;
		} catch (Exception e) {
			return true;
		}
	}
	
	private static boolean isExist( BlogAccount account )
	{
		return isExist( account.getAccountName() );
	}
	
	public static String getLastError()
	{
		return errorString;
	}
}